package com.servlet;

import com.bean.Books;
import com.service.BooksService;
import com.service.impl.BooksServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class BooksServlet extends HttpServlet {
    private BooksService bs = new BooksServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         *方法概述：账户登录验证处理
         */
        String accounts = req.getParameter("account");
        if (accounts != null) {
            getAccounts(req, resp);
        }
        /**
         * 方法概述：手机登录发送验证码
         */
        String phones = req.getParameter("phone");
        String codes = req.getParameter("code");

        if (phones != null && codes == null) {
            getPhones(req, resp);
        }

        /**
         *  方法概述 ;手机登录验证验证码
         */
        if (phones != null && codes != null) {
            getCodes(req, resp);
        }
        /**
         *   账号注册表单
         */
        String accountRigs = req.getParameter("accountRig");
        if (accountRigs != null) {
            getAccountRigs(req, resp);
        }
        /**
         * 手机注册发送验证码
         */
        String phoneRigs = req.getParameter("phoneRig");
        String codeRigs = req.getParameter("codeRig");
        if (phoneRigs != null && codeRigs == null) {
            getPhoneRigs(req, resp);
        }
        /**
         * 手机注册 验证码验证
         */
        if (codeRigs != null && phoneRigs != null) {
            getCodeRigs(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * 方法概述：账户登录表单验证处理
     *
     * @param req
     * @param resp
     */
    private void getAccounts(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//      获取账号和密码信息
        String account = req.getParameter("account");
        String pwd = req.getParameter("pwd");
//      把账号密码封装成对象
        Books books = new Books(account, pwd);
//      设置响应方式
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
//        把数据提交给业务层 并向前端响应信息
        resp.getWriter().println(bs.getAccount(books));

    }

    /**
     * 方法概述：手机登录时验证验证码是否正确
     *
     * @param req
     * @param resp
     */
    private void getPhones(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String phone = req.getParameter("phone");

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().println(bs.up(phone));
    }

    /**
     * 方法概述：手机登录验证
     *
     * @param req
     * @param resp
     */
    private void getCodes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String phone = req.getParameter("phone");
        String code = req.getParameter("code");

        Books books = new Books();
        books.setPhone(phone);
        books.setVeriCode(code);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().println(bs.getCode(books));
    }

    /**
     * 方法概述：账号注册
     *
     * @param req
     * @param resp
     */
    private void getAccountRigs(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String accountRig = req.getParameter("accountRig");
        String pwdRig = req.getParameter("pwdRig");

        Books books = new Books();
        books.setAccount(accountRig);
        books.setPwd(pwdRig);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().println(bs.getAccountRig(books));


    }

    /**
     * 方法概述：手机注册
     *
     * @param req
     * @param resp
     */
    private void getPhoneRigs(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String phoneRig = req.getParameter("phoneRig");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().println(bs.getPoneRig(phoneRig));

    }

    /**
     * 方法概述：手机注册验证
     *
     * @param req
     * @param resp
     */
    private void getCodeRigs(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String phoneRig = req.getParameter("phoneRig");
        String codeRig = req.getParameter("codeRig");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().println(bs.getCodeRig(phoneRig, codeRig));
    }

}
